import moment from 'moment';

const getElapsed = (start, end, time) => {
  let elapsed = 0;
  if (!time) {
    let startMoment = moment(start);
    let endMoment = moment(end);
    if (!start || !end || !startMoment.isValid() || !endMoment.isValid()) {
      return '-';
    }
    if (moment(endMoment).format('X') - moment(startMoment).format('X') > 0) {
      elapsed = endMoment - startMoment;
    }
  } else {
    elapsed = time;
  }
  const tempTime = moment.duration(elapsed);
  const years = tempTime.years();
  const days = tempTime.days();
  const hours = tempTime.hours();
  const minutes = tempTime.minutes();
  const seconds = tempTime.seconds();
  if (years) {
    return `${years}年${days}天${hours}时${minutes}分${seconds}秒`;
  }
  if (days) {
    return `${days}天${hours}时${minutes}分${seconds}秒`;
  }
  return `${hours}时${minutes}分${seconds}秒`;
};

export default getElapsed;
